<template>
    <el-card shadow="never" class="notice">
        <!-- 搜索 -->
        <Search :model="searchForm" @search="getData" @reset="resetSearchForm">
            <SearchItem label="关键词：">
                <el-input v-model="searchForm.keyword" placeholder="管理员昵称" clearable></el-input>
            </SearchItem>
        </Search>
        <!-- 新增|刷新  -->
        <ListHeader @create="handleCreate" @refresh="getData"></ListHeader>
        <el-table :data="tableData" stripe style="width: 100%" v-loading="loading">
            <el-table-column label="管理员">
                <template #default="{ row }">
                    <div class="manager">
                        <el-avatar :size="40" :src="row.avatar">
                            <img src="https://cube.elemecdn.com/e/fd/0fc7d20532fdaf769a25683617711png.png" />
                        </el-avatar>
                        <div class="mangerText">
                            <h6>{{ row.username }}</h6>
                            <small>ID:{{ row.id }}</small>
                        </div>
                    </div>
                </template>
            </el-table-column>
            <el-table-column label="所属管理员" align="center">
                <template #default="{ row }">
                    {{ row.role ? row.role.name : '-' }}
                </template>
            </el-table-column>
            <el-table-column label="状态" width="120">
                <template #default="{ row }">
                    <el-switch :disabled="row.super === 1" :loading="row.statusLoading"
                        @change="handleStatusChange($event, row)" :modelValue="row.status" :active-value="1"
                        :inactive-value="0"></el-switch>
                </template>
            </el-table-column>
            <el-table-column prop="create_time" label="发布时间" width="380" />
            <el-table-column label="操作" width="180" align="center">
                <template #default="scope">
                    <small class="tableAction" v-if="scope.row.super === 1">暂无操作</small>
                    <div v-else>
                        <el-button type="primary" size="small" text @click="handleEdit(scope.row)">修改</el-button>
                        <el-popconfirm title="是否删除该公告" confirmButtonText="确认" cancelButtonText="取消"
                            @confirm.stop="handleDelete(scope.row.id)">
                            <template #reference>
                                <el-button type="primary" size="small" text>删除</el-button>
                            </template>
                        </el-popconfirm>
                    </div>

                </template>
            </el-table-column>
        </el-table>
        <div class="noticeBottom">
            <el-pagination background layout="prev,pager, next" :total="total" :current-page="currentPage"
                :page-size="limit" @current-change="getData" />
        </div>
        <FormDrawer ref="formDrawerRef" :title="drawerTitle" @submit="handleSubmit">
            <el-form :model="form" ref="formRef" :rules='rules' label-width="80px" :inline="false">
                <el-form-item label="用户名" prop="username">
                    <el-input v-model="form.username" placeholder="请输入用户名"></el-input>
                </el-form-item>
                <el-form-item label="密码" prop="password">
                    <el-input v-model="form.password" placeholder="请输入密码" type="textarea" :rows="5"></el-input>
                </el-form-item>
                <el-form-item label="头像" prop="avatar">
                    <ChooseImage v-model="form.avatar" />
                </el-form-item>
                <el-form-item label="所属角色" prop="role_id">
                    <el-select v-model="form.role_id" placeholder="选择所属角色">
                        <el-option v-for="item in roles" :key="item.id" :label="item.name" :value="item.id"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="状态" prop="status">
                    <el-switch v-model="form.status" :active-value="1" :inactive-value="0"></el-switch>
                </el-form-item>
            </el-form>
        </FormDrawer>
    </el-card>
</template>
<script setup>
import { ref } from 'vue'
import FormDrawer from '@/components/FormDrawer.vue'
import ChooseImage from '@/components/ChooseImage.vue'
import ListHeader from '@/components/ListHeader.vue'
import { getManagerList, updateManagerStatus, createManager, updateManager, deleteManager } from '@/api/manager'
import { useInitTable, useInitForm } from '@/hooks/useCommon'
import Search from '@/components/Search.vue'
import SearchItem from '@/components/SearchItem.vue'

// 搜索、获取、分页、删除、修改状态
const roles = ref([])
const {
    searchForm,
    resetSearchForm,
    tableData,
    loading,
    currentPage,
    total,
    limit,
    getData,
    handleDelete,
    handleStatusChange
} = useInitTable({
    searchForm: {
        keyword: ""
    },
    getList: getManagerList,
    onGetListSuccess: (res) => {
        tableData.value = res.list.map(o => {
            o.statusLoading = false
            return o
        })
        roles.value = res.roles
        total.value = res.totalCount
    },
    delete: deleteManager,
    updateStatus: updateManagerStatus
})



// 新增、修改
const {
    formDrawerRef,
    formRef,
    rules,
    form,
    drawerTitle,
    handleSubmit,
    handleCreate,
    handleEdit
} = useInitForm({
    rules: {
        username: [{
            required: true,
            message: '请输入用户名',
            trigger: 'blur'
        }],
        password: [{
            required: true,
            message: '请输入密码',
            trigger: 'blur'
        }],
        role_id: [{
            required: true,
            message: '请选择所属角色',
            trigger: 'change'
        }],
        avatar: [{
            required: true,
            message: '请选择头像',
            trigger: 'change'
        }]
    },
    form: {
        username: '',
        password: '',
        role_id: null,
        status: 1,
        avatar: ""
    },
    getData,
    update: updateManager,
    create: createManager
})


</script>
<style scoped lang="less">
.notice {
    border: 0;



    .tableAction {
        font-size: 14px;
    }

    .noticeBodySearch {
        margin-bottom: 10px;

        .noticeBodySearchBtn {
            display: flex;
            justify-content: end;
            align-items: center;
        }
    }

    .manager {
        display: flex;
        align-items: center;

        .mangerText {
            margin-left: 3px;
        }
    }

    .noticeBottom {
        display: flex;
        align-items: center;
        justify-content: center;
        margin-top: 10px;
    }
}
</style>